<?php

//query to fill up the drop-down box
$username="bsc353_4";
$password="sonbeans";
$database="bsc353_4";

mysql_connect("clipper.encs.concordia.ca",$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query = "SELECT * FROM Book;";
$result = mysql_query($query) or die ("Query unsuccessful");


//populate the fields. If the page posts back to itself, these will be filled
if(isset($_POST['selection']))
{
    $ISBN =  $_POST['selection'];

    $queryFill = "SELECT * FROM Book WHERE ISBN = '$ISBN';";
    $resultFill = mysql_query($queryFill) or die ("Failed to fill fields from database");
   // echo $resultFill;
    $i=0;
    $num=mysql_numrows($resultFill);
    
    $price= mysql_result($resultFill, 0, "price");
    $price = ($price % 100) + $price/100; 

    $year= mysql_result($resultFill, 0, "year");
    $author=mysql_result($resultFill, 0, "author");
    $subject=mysql_result($resultFill, 0, "subject");
    $publisher=mysql_result($resultFill, 0, "publisher");
    $title=mysql_result($resultFill, 0, "title");
    $quantity= mysql_result($resultFill, 0, "yearToDateQtySold");                
}



?>



<html>
<head>
<title>Edit Book Data</title>
<style type="text/css">
body {
	font-family:verdana,arial,sans-serif;
	font-size:10pt;
	margin:30px;
	background-color:#ffcc00;
	}
</style>
</head>
<body>
<h1>Edit Book Data</h1>
<form name = "chooseBook" action="content.php" method = "post">
<select name = "selection" onChange = "onChangeSelect"> 
    <?php
    $i=0;
    $num=mysql_numrows($result);
    while ($i < $num) 
    { 
        $vartitle = mysql_result($result, $i, "title");
        $varISBN = mysql_result($result, $i, "ISBN");
        echo  "<option value='$varISBN'";
        if(isset($_POST['selection']) && $varISBN == $ISBN)
        {
            echo "selected = 'selected'";
        }    
	echo  ">$vartitle </option>";
             
        $i++; 
    }
    ?>
</select>
<input type="Submit" value = "Select book">
</form>

<form name= "editbook" action="editbook_update.php" method="post">



<br>

<?php
if(isset($_POST['selection'])) //set the fields based on the book selection
{
    echo "<table>";
    echo "<tr><td style='text-align:right'>Price: </td><td><input type='text' name='price' value= '$price'></td></tr>";
    echo "<tr><td style='text-align:right'>Year: </td><td><input type='text' name='year' value = '$year'></td></tr>";
    echo "<tr><td style='text-align:right'>ISBN: </td><td><input type='text' name='ISBN' value = '$ISBN'></td></tr>";
    echo "<tr><td style='text-align:right'>Author: </td><td><input type='text' name='author' value = '$author'></td></tr>";
    echo "<tr><td style='text-align:right'>Subject: </td><td><input type='text' name='subject' value = '$subject'></td></tr>";
    echo "<tr><td style='text-align:right'>Publisher: </td><td><input type='text' name='publisher' value = '$publisher'></td></tr>";
    echo "<tr><td style='text-align:right'>Title: </td><td><input type='text' name='title' value = '$title'></td></tr>";
    echo "<tr><td style='text-align:right'>Quantity sold to date: </td><td><input type='text' name='quantity' value = '$quantity'></td></tr>";
    echo "<input type='hidden' name='origISBN' value='$ISBN' />";
}
else
{
    echo "<table>";
    echo "<tr><td style='text-align:right'>Price: </td><td><input type='text' name='price' value= '1'></td></tr>";
    echo "<tr><td style='text-align:right'>Year: </td><td><input type='text' name='year' value = ''></td></tr>";
    echo "<tr><td style='text-align:right'>ISBN: </td><td><input type='text' name='ISBN' value = ''></td></tr>";
    echo "<tr><td style='text-align:right'>Author: </td><td><input type='text' name='author' value = ''></td></tr>";
    echo "<tr><td style='text-align:right'>Subject: </td><td><input type='text' name='subject' value = ''></td></tr>";
    echo "<tr><td style='text-align:right'>Publisher: </td><td><input type='text' name='publisher' value = ''></td></tr>";
    echo "<tr><td style='text-align:right'>Title: </td><td><input type='text' name='title' value = ''></td></tr>";
    echo "<tr><td style='text-align:right'>Quantity sold to date: </td><td><input type='text' name='quantity' value = '0'></td></tr>";
  
}
?>


<tr><td style='text-align:center'><input type="Submit" name = "delete" value = "Delete"></td>
<td style='text-align:center'><input type="Submit" name = "save" value = "Save Changes"></td></tr>
</table>

</form> 


</body>
</html>